package com.example.hao.smssdktest;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;
import cn.smssdk.gui.RegisterPage;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "MainActivity";
    private EditText et_phone;
    private EditText et_code;
    private Button btn_getCode;
    private Button btn_confirm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        initView();
//        SMSSDK.registerEventHandler(eh); //注册短信回调
        sendCode(this);
    }

    private void initView() {
        et_phone = findViewById(R.id.et_phone);
        et_code = findViewById(R.id.et_code);
        btn_getCode = findViewById(R.id.btn_getCode);
        btn_confirm = findViewById(R.id.btn_confirm);
        btn_getCode.setOnClickListener(this);
        btn_confirm.setOnClickListener(this);

    }

    EventHandler eh = new EventHandler() {
        @Override
        public void afterEvent(int event, int result, Object data) {
            Log.d(TAG, "afterEvent: " + event);
            Log.d(TAG, "afterEvent: " + result);
            Log.d(TAG, "afterEvent: " + data);

            if (result == SMSSDK.RESULT_COMPLETE) {
                //回调完成
                if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                    //提交验证码成功
                    Log.d("SB", "提交验证码成功");
                } else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE) {
                    //获取验证码成功
                    Log.d("SB", "获取验证码成功");
                } else if (event == SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES) {
                    //返回支持发送验证码的国家列表
                }
            } else {
                ((Throwable) data).printStackTrace();
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_getCode:
                SMSSDK.getVerificationCode("86", et_phone.getText().toString());
                break;
            case R.id.btn_confirm:
                SMSSDK.submitVerificationCode("86", et_phone.getText().toString(), et_code.getText().toString());
                break;
        }
    }

    //带GUI的版本.
    public void sendCode(Context context) {
        RegisterPage page = new RegisterPage();
        //如果使用我们的ui，没有申请模板编号的情况下需传null
        page.setTempCode(null);
        page.setRegisterCallback(new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
                if (result == SMSSDK.RESULT_COMPLETE) {
                    // 处理成功的结果
                    HashMap<String, Object> phoneMap = (HashMap<String, Object>) data;
                    String country = (String) phoneMap.get("country"); // 国家代码，如“86”
                    String phone = (String) phoneMap.get("phone"); // 手机号码，如“13800138000”
                    // TODO 利用国家代码和手机号码进行后续的操作

                    Log.d("TAG", "afterEvent: " + country + "," + phone);
                } else {
                    // TODO 处理错误的结果
                }
            }
        });
        page.show(context);
    }
}
